import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;

public class testInter {
    @Test
    public void getIP() throws UnknownHostException {
        System.out.println(InetAddress.getLocalHost());
        System.out.println(InetAddress.getAllByName("cxa299"));
        System.out.println(Inet4Address.getLocalHost());
        System.out.println(Inet6Address.getLocalHost());
        System.out.println(Inet6Address.getByName("www.baidu.com"));
        System.out.println(Inet4Address.getByName("www.baidu.com"));
        System.out.println(InetAddress.getByName("www.baidu.com"));
    }

    @Test
    public void getSocket() {
        InetSocketAddress inetSocketAddress = new InetSocketAddress("cxa299", 8080);
        System.out.println(inetSocketAddress);
        System.out.println(inetSocketAddress.getAddress());
    }

    @Test
    public void tcpClientDemo01() throws IOException {
        InetAddress.getLocalHost();
        int port = 8888;
        Socket socket  =new Socket( InetAddress.getLocalHost(),port);
        socket.getOutputStream().write("hello world".getBytes());
        //socket.close();
    }

    @Test
    public void tcpServerDemo01() throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        Socket socket = serverSocket.accept();
        InputStream inputStream = socket.getInputStream();
        int by;
        while((by = inputStream.read())!=-1){
            System.out.println((char) by);
        }
    }
}
